﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CRM.Models.Email;
using Microsoft.EntityFrameworkCore;

namespace CRM.Services.Email
{
    public class EmailMarketingLogService : BaseService
    {
        public EmailMarkeingLog Create()
        {
            
            return null;
        }

        public async Task<List<EmailMarkeingLog>> GetInfo(int id)
        {
            var result = await DbContext.EmailMarkeingLog.Where(m => m.CompanyId == CurrentUser.CompanyId).Where(m => m.EmailMarketingId == id).OrderByDescending(m => m.Id).ToListAsync();
            return result;
        }
        
        public bool EditTimeTransmission(int id,DateTime timeNew)
        {
            var recordInfo =  DbContext.EmailMarketing.FirstOrDefault(m => m.Id == id);
            if (recordInfo!=null && recordInfo.IsLocked == false && recordInfo.State != EmailMarketing.StateEnum.Completed && recordInfo.State != EmailMarketing.StateEnum.Canceled)
            {
                recordInfo.TimingTransmission = timeNew;
                DbContext.SaveChanges();
                return true;
            }
            return false;
        }
        
        public EmailMarketing GetInfoById(int id)
        {
            var result = DbContext.EmailMarketing.FirstOrDefault(m => m.Id == id);
            return result;
        }
        
        public bool SetState(int id,EmailMarketing.StateEnum state)
        {
            var info = DbContext.EmailMarketing.FirstOrDefault(m => m.Id == id);
            info.State = state;
            var res = DbContext.SaveChanges();
            return res > 0;
        }
        
    }
}